/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月23日
 * @version 0.1
 */
package com.suredy.base.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.base.model.Permission;
import com.suredy.base.model.User2Permission;
import com.suredy.core.service.BaseSrvWithEntity;


/**
 * @author ZhangMaoren
 *
 */
@Service("User2PermissionSrv")
public class User2PermissionSrv extends BaseSrvWithEntity<User2Permission>{
	@Override
	public DetachedCriteria getDc(User2Permission t) {
		DetachedCriteria dc = super.getDc(t);

		if (t == null) {
			return dc;
		}
		
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		
		if (t.getUserPermission() != null) {
			dc.add(Restrictions.eq("userPermission", t.getUserPermission()));
		}
		if (!StringUtils.isBlank(t.getUserId())) {
			dc.add(Restrictions.eq("userId", t.getUserId()));
		}

		return dc;
	}
	
	public List<User2Permission> getAll() {
		return this.readByEntity(null);
	}

	public User2Permission get(String id) {
		return this.get(id);
	}
	
	public List<User2Permission> getByPermission(String permissionId) {
		List<User2Permission> ret = new ArrayList<User2Permission>(); 
		if (StringUtils.isBlank(permissionId)) {
			return ret;
		}
		User2Permission search = new User2Permission();
		Permission permission = new Permission();
		permission.setId(permissionId);
		search.setUserPermission(permission);
		ret = this.readByEntity(search);
		return ret;
	}
	
	public List<User2Permission> getByUser(String userId) {
		List<User2Permission> ret = new ArrayList<User2Permission>(); 
		if (StringUtils.isBlank(userId)) {
			return ret;
		}
		User2Permission search = new User2Permission();
		search.setUserId(userId);
		ret = this.readByEntity(search);
		return ret;
	}

}
